<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :parent-list="parentList"
      :design-state="designState"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field?.options?.displayStyle"
  >
    <div class="example-widget" :style="setStyle('area')">
      示例
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from 'uuid';
import emitter from '@/utils/designer/emitter'
import i18n, {translate} from "@/utils/vue-i18n/i18n";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import {pxToVw} from "@/utils/common";

export default {
  name: "example-widget",

  //必须固定为FieldWidget，用于接收父级组件的broadcast事件
  componentName: "FieldWidget",

  mixins: [emitter, mixin, i18n],

  props: {
    field: Object,
    widget: Object,
    designer: Object,
    parentList: Array,
    parentWidget: Object,
    indexOfParentList: Number,
    devType: {
      type: Boolean,
      default: () => {
        return false
      },
    },
    designState: {
      type: Boolean,
      default: false
    },
    subFormRowIndex: { /* 子表单组件行索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormColIndex: { /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1
    },
    /* 子表单组件行Id，唯一id且不可变 */
    subFormRowId: {
      type: String,
      default: ''
    },
  },

  components: {
    CompOperate,
  },

  data() {
    return {
      fieldOptionsInfo:Object(),
      widgetKey: uuid(),
      optionsInfo: Object(),
    }
  },

  computed: {
    setStyle(){
      return function (type) {
        const self = this;
        let {areaInfo} = (self.fieldOptionsInfo) || Object();
        let Obj = Object();
        switch (type) {
          case "area":
            Reflect.set(Obj, "height", pxToVw(areaInfo?.height));
            Reflect.set(Obj, "background", (areaInfo?.background));
            Reflect.set(Obj, "fontSize", pxToVw(areaInfo?.fontSize));
            Reflect.set(Obj, "fontWeight", (areaInfo?.fontWeight));
            Reflect.set(Obj, "color", pxToVw(areaInfo?.color));
            return Obj;
        }
      }
    }
  },

  beforeCreate() {
    /* 这里不能访问方法和属性！！ */
  },

  created() {
    /*
      注意：子组件mounted在父组件created之后、父组件mounted之前触发;
      故子组件mounted需要用到的prop需要在父组件created中初始化！！
    */
    this.registerToRefList()
    this.initEventHandler()
    this.handleOnCreated()
  },

  mounted() {
    this.handleOnMounted()
  },

  methods: {},

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        this.fieldOptionsInfo = val.options;
        let key = (val.options.config);
        if (!!key) {
          this.optionsInfo = val.options[key];
        }
      },
      deep: true,
      immediate: true
    }
  },

  beforeUnmount() {
    this.unregisterFromRefList()
  },
}
</script>

<style lang="scss" scoped>
.example-widget {
  background: transparent;
  font-size: 80px;
  background: red;
}
</style>